<template>
  <div class="header flex-row-between">
    <div class="flex-row-around">
      <i :class="`el-icon-s-${menuState} f-Elarge m-r-10`" id="menuSwitch" @click="menuSwitch"></i>
      <Title class="text-c-white"></Title>
    </div>
    <div class="loginInfo">
      <img src="@/assets/user/avatar-2.jpg" alt="" />
      <DropDown @command="handleDropDown" trigger="click" :items="dropDownConfig">
        <template slot="title">
          <span class="el-dropdown-link">
            管理员{{ userInfo.username }}
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
        </template>
      </DropDown>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "Header",
  data() {
    return {
      menuState: "fold",
      dropDownConfig: [
        { label: '个人信息', method: 'toUserInfo', icon: 'el-icon-s-custom' },
        { label: '退出登录', method: 'LogOut', icon: 'el-icon-switch-button' }
      ]
    };
  },
  computed: {
    ...mapState("userHandle", ["userInfo"]),
  },
  methods: {
    handleDropDown(method) {
      this[method]();
    },
    // 菜单栏的隐藏与展示
    menuSwitch() {
      this.$store.commit("menuSwitch/switch");
      this.menuState = this.menuState.indexOf("un") == -1 ? "unfold" : "fold";
    },
    toUserInfo() {
      this.$router.replace({
        path: "/manage",
      });
    },
    // 退出登录
    async LogOut() {
      this.$store
        .dispatch("userHandle/toUserLogOut")
        .then(() => {
          this.$router.replace({
            path: "/login",
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style scoped lang="less">
.header {
  .loginInfo {
    img {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      margin: 0 10px;
    }

    :deep(.el-dropdown-link) {
      cursor: pointer;
      color: #fff;
    }

    .el-icon-arrow-down {
      font-size: 12px;
    }
  }

  #menuSwitch:hover {
    transition: all 0.1s;
    transform: scale(1.1);
    color: #3de8fe;
    cursor: pointer;
  }
}
</style>